﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Collections;
using System.Runtime.InteropServices;//引用命名空间

using ctdReader;
using ctdFunction;

namespace JsRunTool
{
    public class CPublic : Function
    {
        delegate int FCtdRunJsScript(int sChannelID, int iNum, string jsPath,string inData, StringBuilder outData);

        delegate int FMakeCard(int sChannelID, int iNum, string dataBuf, StringBuilder retBuf);
        public delegate int CardCommandTypeDelegate(IntPtr reader, ref UInt32 len,
                [In, Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 300)]byte[] buffer);

        public static CardCommandTypeDelegate g_commd;
        public static CardCommandTypeDelegate g_reset;

        public static Form1 fm;
        public static  CardDllInterface m_card;
        public static  Script sc = new Script();

        public static CardDllInterface getCardDllInterface()
        {
            return m_card;
        }

        public static void setCardDllInterface(ref CardDllInterface _cardDllInterFace )
        {
            m_card = _cardDllInterFace;
        }

        private static int dllsendcmd(IntPtr reader, ref UInt32 len, [In, Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 300)]byte[] buffer)
        {
            string apdu = ByteToHexStr(buffer, (int)len);
            long ret = m_card.pCardReader.sendApdu(apdu);
            string rsp = m_card.pCardReader.getResp() + m_card.pCardReader.getSw1Sw2();
            fm.printApdu(apdu + "=" + rsp);

            byte[] rspByte = HexStrToByte(rsp);
            Array.Copy(rspByte, buffer, rsp.Length / 2);
            len = (UInt32)rsp.Length / 2;
            return (int)ret;
        }

        private static int dllreset(IntPtr reader, ref UInt32 len, [In, Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 300)]byte[] buffer)
        {
            int ret = m_card.pCardReader.cardReset();
            string atr = m_card.pCardReader.getResp();
            fm.printApdu("reset" + "=" + atr);

            byte[] bufatr = Function.strToToHexByte(atr);
            Array.Copy(bufatr, buffer, atr.Length / 2);
            len = (UInt32)(atr.Length / 2);
            return ret;
        }
        public delegate int LockOrUnLockType();
        public static LockOrUnLockType lockF;
        public static int LockOrUnLockTypeF()
        {
            return 1;
        }

        delegate int FmakeCardBegin(CardCommandTypeDelegate send, CardCommandTypeDelegate reset,
            LockOrUnLockType lock1, LockOrUnLockType lock2);

        public static int checkCard(string dllPath,string funName,string data,StringBuilder retStr)
        {
            //1 获得句柄
            int hModule = DllnSpace.DLLWrapper.LoadLibrary(dllPath);
            if (hModule == 0) {
                return 0;
            }

            //2、获取相应的委托实例：
            FmakeCardBegin fmakeCardBegin = (FmakeCardBegin)DllnSpace.DLLWrapper.GetFunctionAddress(hModule, "CtdMakeCardBegin", typeof(FmakeCardBegin));
            if (fmakeCardBegin == null) {
                DllnSpace.DLLWrapper.FreeLibrary(hModule);
                return 0;
            }

            FMakeCard fmakeCard = (FMakeCard)DllnSpace.DLLWrapper.GetFunctionAddress(hModule, funName, typeof(FMakeCard));
            if (fmakeCard == null) {
                DllnSpace.DLLWrapper.FreeLibrary(hModule);
                return 0;
            }
            g_commd = dllsendcmd;
            g_reset = dllreset;
            lockF = LockOrUnLockTypeF;

            //3 运行函数
            fmakeCardBegin(g_commd, g_reset, lockF, lockF);

            int ret = fmakeCard(1, 1, data, retStr);

            //4 释放句柄
            DllnSpace.DLLWrapper.FreeLibrary(hModule);

            return ret;
        }

        public static int runJs(string jsPath, string inData, StringBuilder outData)
        {
            //1 获得句柄
            int hModule = DllnSpace.DLLWrapper.LoadLibrary("./JsScriptRun.dll");
            if (hModule == 0)
            {
                return 0;
            }

            //2、获取相应的委托实例：
            FmakeCardBegin fmakeCardBegin = (FmakeCardBegin)DllnSpace.DLLWrapper.GetFunctionAddress(hModule, "CtdMakeCardBegin", typeof(FmakeCardBegin));
            if (fmakeCardBegin == null)
            {
                DllnSpace.DLLWrapper.FreeLibrary(hModule);
                return 0;
            }

            FCtdRunJsScript fCtdRunJsScript = (FCtdRunJsScript)DllnSpace.DLLWrapper.GetFunctionAddress(hModule, "CtdRunJsScript", typeof(FCtdRunJsScript));
            if (fCtdRunJsScript == null)
            {
                DllnSpace.DLLWrapper.FreeLibrary(hModule);
                return 0;
            }
            g_commd = dllsendcmd;
            g_reset = dllreset;
            lockF = LockOrUnLockTypeF;

            if (m_card.pCardReader.cardReset() != 0)
            {
                fm.printApdu("复位失败,请放入卡片!");
                DllnSpace.DLLWrapper.FreeLibrary(hModule);
                return 0;
            }
            string atr = m_card.pCardReader.getResp();

            //3 运行函数
            fmakeCardBegin(g_commd, g_reset, lockF, lockF);

            int ret = fCtdRunJsScript(1, 1, jsPath, inData, outData);

            //4 释放句柄
            DllnSpace.DLLWrapper.FreeLibrary(hModule);
            return ret;
        }

        public static void test()
        {
            sc.reset();
        }
    }
}
